import { isFunction } from '@/common/helper/is'
import $store from '@/store'

export const JoinTimestamp = (join = true, restful = false) => {
  if (!join) {
    return restful ? '' : {}
  }
  const now = new Date().getTime()
  if (restful) {
    return `?_t=${now}`
  }
  return { _t: now }
}

export const MessageFun = (Custom, Message, isSuccess) => {
  if (Custom.showMessage && Message) {
    switch (Custom.messageMode) {
      case 'modal':
        uni.showModal({
          title: '提示',
          content: Message,
          success: (res) => {
            // 模态'取消'||'确定' 回调
            if (Custom.modalCallBack && isFunction(Custom.modalCallBack)) return Promise.resolve(Custom.modalCallBack(res))
          },
        })
        break

      default:
        uni.showToast({ title: Message, icon: isSuccess ? Custom.toastIcon : 'none', mask: true })
        break
    }
  }
}

export const AuthError = () => {
  if ($store.getters.isLogin) $store.dispatch('user/Logout')
  uni.$Auth()
}
